package com.lv.fast.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lv.fast.DTO.SysMenuDTO;
import com.lv.fast.annotation.SysLog;
import com.lv.fast.enums.OperateTypeEnum;
import com.lv.fast.model.RestResult;
import com.lv.fast.service.SysMenuService;
import com.lv.fast.valid.group.AddStrategy;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashSet;

/**
 * @Description
 * @Author jie.lv
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sys/menu")
@Api(tags = "后台管理---菜单模块")
@ApiSupport(author = "lv")
public class SysMenuController {

    private final SysMenuService sysMenuService;

    @GetMapping("/list/byParent/{parentId}")
    @ApiOperation(value = "根据上级Id获取菜单列表")
    @ApiOperationSupport(order = 1)
    @RequiresPermissions("sys:menu:list")
    public RestResult listByParentId(@PathVariable String parentId){
        LinkedHashSet<SysMenuDTO> sysMenuDTOS = sysMenuService.listByParentId(parentId);
        return RestResult.success(sysMenuDTOS);
    }

    @GetMapping("/get/{id}")
    @ApiOperation(value = "查询菜单详情")
    @ApiOperationSupport(order = 2)
    @RequiresPermissions("sys:menu:info")
    public RestResult get(@PathVariable String id){
        SysMenuDTO sysMenuDTO = sysMenuService.get(id);
        return RestResult.success(sysMenuDTO);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增菜单")
    @ApiOperationSupport(order = 3)
    @RequiresPermissions("sys:menu:insert")
    @SysLog(operateType = OperateTypeEnum.INSERT, value = "新增菜单")
    public RestResult add(@RequestBody @Validated(AddStrategy.class) SysMenuDTO menuDTO){
        return RestResult.build(sysMenuService.add(menuDTO));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除菜单")
    @ApiOperationSupport(order = 4)
    @RequiresPermissions("sys:menu:delete")
    @SysLog(operateType = OperateTypeEnum.DELETE, value = "删除菜单")
    public RestResult delete(@PathVariable String id){
        return RestResult.build(sysMenuService.delete(id));
    }
}
